package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.utils.CurrentUserUtil;
import com.example.dao.ClassifyDao;
import com.example.entity.domain.Classify;
import com.example.entity.dto.BaseDTO;
import com.example.entity.vo.ClassifyVo;
import com.example.service.ClassifyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: tjatse
 * @Date: 2025/05/07/21:40
 * @Description:
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Resource
    private ClassifyDao classifyDao;

    @Override
    public Page<ClassifyVo> list(BaseDTO baseDTO) {
        // 1. 分页查询当前页数据
        Page<Classify> page = new Page<>(baseDTO.getPageNum(), baseDTO.getPageSize());
        Page<Classify> pageList = classifyDao.pageList(page, baseDTO);
        List<Classify> records = pageList.getRecords();

        // 2. 查询所有分类数据并转为 Map，便于通过 id 快速查找
        List<Classify> classifies = classifyDao.selectList(new QueryWrapper<>());
        Map<Integer, Classify> classifyMap = classifies.stream()
                .collect(Collectors.toMap(Classify::getId, Function.identity()));

        // 3. 将当前页的记录转换为 VO 并组装成树形结构
        List<ClassifyVo> voList = records.stream()
                .map(this::convertToTree)
                .toList();

        // 4. 构造返回的 Page
        Page resultPage = new Page<>(baseDTO.getPageNum(), baseDTO.getPageSize(), page.getTotal());
        resultPage.setRecords(voList);
        return resultPage;
    }



    private ClassifyVo convertToTree(Classify classify) {
        ClassifyVo vo = new ClassifyVo();
        vo.setId(classify.getId());
        vo.setName(classify.getName());
        vo.setParentId(classify.getParentId());
        vo.setLevel(classify.getLevel());
        vo.setDescription(classify.getDescription());
        vo.setCreatedBy(classify.getCreatedBy());
        vo.setUpdatedBy(classify.getUpdatedBy());
        vo.setCreatedTime(classify.getCreatedTime());
        vo.setUpdatedTime(classify.getUpdatedTime());
        Classify parent = classifyDao.selectById(classify.getParentId());
        if (parent != null){
            vo.setParentName(parent.getName());
        }else {
            vo.setParentName("");
        }
        // 递归查找子节点
        List<ClassifyVo> children = new ArrayList<>();
        for (Classify child : classifyDao.selectList(new QueryWrapper<Classify>().eq("parent_id", classify.getId()))) {
            children.add(convertToTree(child));
        }
        vo.setChildren(children);

        return vo;
    }

    @Override
    public Boolean add(Classify classify) {
        String username = CurrentUserUtil.getCurrentUsername();
        classify.setCreatedBy(username);
        classify.setUpdatedBy(username);
        classify.setCreatedTime(LocalDateTime.now());
        classify.setUpdatedTime(LocalDateTime.now());
        return classifyDao.insert(classify) > 0;
    }

    @Override
    public Boolean update(Classify classify) {
        String username = CurrentUserUtil.getCurrentUsername();
        classify.setUpdatedBy(username);
        classify.setUpdatedTime(LocalDateTime.now());
        return classifyDao.updateById(classify) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return classifyDao.deleteById(id) > 0;
    }
}
